import { createRouter, createWebHistory } from 'vue-router'
import { routeLazy } from '@/utils/routeLazy'

const LayoutIndex = () => import('@/components/Layout/LayoutIndex.vue')

/* 无需布局的页面 */
const constantRoutes = [
  {
    path: '/login',
    component: routeLazy('login/LoginIndex'),
    hidden: true,
  },
  {
    path: '/404',
    component: routeLazy('errors/NotFound'),
    hidden: true,
  },
]
/* 需要布局的动态/权限菜单 */
const asyncRoutes = [
  {
    path: '/',
    component: LayoutIndex, // 关键：挂载布局
    redirect: '/kbManager',
    children: [
      {
        path: 'dashboardManager',
        name: 'DashboardBaseManager',
        component: routeLazy('dashboard/dashboardBase'),
        redirect: '/dashboard',
        meta: { title: '首页', icon: 'Odometer' },
        children: [
          {
            path: '/dashboard',
            name: 'dashboard',
            component: routeLazy('dashboard/dashboardView'),
            meta: { title: '全部仪表盘', icon: 'Book' },
          },
        ],
      },

      {
        path: 'componentsLibManager',
        name: 'ComponentsLiBaseManager',
        component: routeLazy('ComponentsLibrary/componentsBase'),
        redirect: '/componentsLib',
        meta: { title: '组件库管理', icon: 'components' },
        children: [
          {
            path: '/componentsLib',
            name: 'componentsLib',
            component: routeLazy('ComponentsLibrary/componentsLibrary'),
            meta: { title: '全部组件', icon: 'Book' },
          },
        ],
      },
      {
        path: 'favoritesManager',
        name: 'FavoritesPageBaseManager',
        component: routeLazy('FavoritesPage/favoritesBase'),
        redirect: '/favorites',
        meta: { title: '收藏管理', icon: 'Star' },
        children: [
          {
            path: '/favorites',
            name: 'favorites',
            component: routeLazy('FavoritesPage/favoritesPage'),
            meta: { title: '全部收藏', icon: 'Book' },
          },
        ],
      },
      {
        path: 'kbManager',
        name: 'KnowledgeBaseManager',
        redirect: '/allDocuments',
        component: routeLazy('kbManager/knowledgeBase'),
        meta: { title: '知识库管理', icon: 'Document' },
        children: [
          {
            path: '/allDocuments',
            name: 'AllDocuments',
            component: routeLazy('kbManager/allDocuments'),
            meta: { title: '全部文档', icon: 'Book' },
          },
          {
            path: '/design',
            name: 'DesignGuide',
            component: routeLazy('kbManager/designGuide'),
            meta: { title: '设计指南', icon: 'PencilRuler' },
          },
          {
            path: '/development',
            name: 'DevelopmentGuide',
            component: routeLazy('kbManager/developmentGuide'),
            meta: { title: '开发指南', icon: 'Code' },
          },
          {
            path: '/api',
            name: 'ApiDocumentation',
            component: routeLazy('kbManager/apiDocumentation'),
            meta: { title: 'API文档', icon: 'FileTextO' },
          },
          {
            path: '/operations',
            name: 'OperationsGuide',
            component: routeLazy('kbManager/operationsGuide'),
            meta: { title: '运维文档', icon: 'Cogs' },
          },
          {
            path: '/project',
            name: 'ProjectPlan',
            component: routeLazy('kbManager/projectPlan'),
            meta: { title: '项目计划', icon: 'Tasks' },
          },
        ],
      },
    ],
  },
]

const router = createRouter({
  // 使用 createWebHistory 创建基于 HTML5 history 模式的路由历史记录
  // import.meta.env.BASE_URL 用于获取环境变量中的基础路径
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...asyncRoutes,
    ...constantRoutes,
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
  ],
})

export default router
